package com.dingreading.cloud.soms.service.impl;

import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.soms.entity.CaTimes;
import com.dingreading.cloud.soms.entity.table.CaTimesTableDef;
import com.dingreading.cloud.soms.mapper.CaTimesMapper;
import com.dingreading.cloud.soms.service.CaTimesService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class CaTimesServiceImpl extends BaseServiceImpl<CaTimesMapper, CaTimes, CaTimesTableDef> implements CaTimesService {

    @Override
    protected CaTimesTableDef getTable() {
        return CaTimesTableDef.caTimes;
    }

    @Override
    public Page<CaTimes> pageList(PageUtil pageUtil, String storeUid, String keyword, Integer enabled) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(storeUid))
            condition.and(table.storeUid.eq(storeUid));
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.beginTime.like(keyword)
                    .or(table.endTime.like(keyword)));
        if (null != enabled)
            condition.and(table.enabled.eq(enabled));

        QueryWrapper wrapper = QueryWrapper.create().where(condition)
                .orderBy(table.id.desc());

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

    @Override
    public List<CaTimes> getByStoreUid(String storeUid, String keyword, Integer enabled) {
        QueryCondition condition = table.storeUid.eq(storeUid);
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.beginTime.like(keyword)
                    .or(table.endTime.like(keyword)));
        if (null != enabled)
            condition.and(table.enabled.eq(enabled));
        return getMapper().selectListByCondition(condition);
    }

    @Override
    public CaTimes getBean(String storeUid, Long id) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.id.eq(id));
        return getMapper().selectOneByCondition(condition);
    }

    @Override
    public boolean haveTime(String storeUid, Long id, String beginTime, String endTime) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.beginTime.eq(beginTime))
                .and(table.endTime.eq(endTime));
        if (null != id && id > 0)
            condition.and(table.id.ne(id));
        return exists(condition);
    }

    @Override
    public int dropBean(String storeUid, Long id) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.id.eq(id));
        return getMapper().deleteByCondition(condition);
    }

    @Override
    public boolean updateEnabled(String storeUid, Long id, Integer enabled, String staffUid, String staffName) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.id.eq(id));
        return UpdateChain.of(CaTimes.class)
                .set(table.enabled, enabled)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(condition)
                .update();
    }
}
